<?php namespace Phpcmf\Controllers;

class Home extends \Phpcmf\App
{

    public function index() {

        // 模型类
        $search = \Phpcmf\Service::M('search', APP_DIR)->init('');

        // 搜索参数
        $config = \Phpcmf\Service::M('app')->get_config('huiyuan');
        list($groupid, $get) = $search->get_param($config);
        $groupid = intval($groupid);


        // 搜索数据
        $data = $search->get_user_data($config, $get, $groupid);

        isset($data['code']) && $data['code'] == 0 && $data['msg'] && exit($this->_msg(0, $data['msg']));
        unset($data['params']['page']);

        $list = [];
        $where = urlencode($data['contentid']);
        if (IS_API_HTTP && $data['id']) {
            // 移动端请求时
            if ($where) {
                $pagesize = intval(\Phpcmf\Service::L('input')->request('pagesize'));
                $tag = 'member where='.$where.' order='.$data['params']['order'].' more=1 page=1 pagesize='.$pagesize.' urlrule=test';
                $rt = \Phpcmf\Service::V()->list_tag($tag);
                $list = $rt['return'];
            }
        }

        // 分页地址
        $urlrule = dr_hx_search_url($data['params'], 'page', '{page}');
        // 识别自定义地址，301定向
        if (strpos(FC_NOW_URL, 'index.php') !== false && strpos($urlrule, 'index.php') === false) {
            $get['page'] > 1 && $data['params']['page'] = $get['page'];
            dr_redirect(dr_hx_search_url($data['params']), 'auto', 301);exit;
        }

        \Phpcmf\Service::V()->assign($this->_format_search_seo($config, $groupid, $data['params'], $get['page']));
        \Phpcmf\Service::V()->assign(array(
            'get' => $get,
            'list' => $list,
            'where' => $where,
            'params' => $data['params'],
            'groupid' => $groupid,
            'keyword' => $data['keyword'],
            'urlrule' => $urlrule,
            'search_sql' => $data['sql'],
            'is_search_page' => 1,
        ));
        \Phpcmf\Service::V()->module(APP_DIR);

        if (isset($_GET['ajax_page']) && $_GET['ajax_page']) {
            $tpl = dr_safe_filename($_GET['ajax_page']);
        } else {
            $tpl = 'search.html';
        }

        \Phpcmf\Service::V()->display($tpl);
    }

    function _format_search_seo($config, $groupid, $param, $page = 1) {

        $seo = [];
        $seo['meta_keywords'] = '';

        $data['page'] = $page > 1 ? $page : '';
        $data['join'] = SITE_SEOJOIN ? SITE_SEOJOIN : '_';
        $data['param'] = '';
        $data['keyword'] = '';

        $param_value = [];

        if ($groupid) {
            $param_value[] = $this->member_cache['group'][$groupid]['name'];
            unset($param['groupid']);
        }

        if ($param['keyword']) {
            $data['keyword'] = $param['keyword'];
            $seo['meta_keywords'].= $data['keyword'].',';
            unset($param['keyword']);
        }

        if ($param) {
            $myfield = $this->member_cache['field'];
            foreach ($param as $name => $value) {
                if (!isset($myfield[$name])) {
                    unset($param[$name]);
                    continue;
                }
                switch ($myfield[$name]['fieldtype']) {

                    case 'Radio':
                    case 'Select':
                    case 'Checkbox':
                        $opt = dr_format_option_array($myfield[$name]['setting']['option']['options']);
                        $arr = explode('|', $value);
                        if ($arr) {
                            foreach ($arr as $a) {
                                isset($opt[$a]) && $opt[$a] && $param_value[] = $opt[$a];
                            }
                        }
                        break;

                    case 'Linkages':
                    case 'Linkage':
                        $arr = explode('|', $value);
                        if ($arr) {
                            foreach ($arr as $a) {
                                $param_value[] = dr_linkagepos($myfield[$name]['setting']['option']['linkage'], $a, $data['join']);
                            }
                        }
                        break;

                    default:
                        $value && $param_value[] = $value;
                        break;

                }
            }
        }

        if ($param_value) {
            $data['param'] = implode($data['join'], $param_value);
            $seo['meta_keywords'].= implode(',', $param_value).',';
        }

        $meta_title = $config['search']['title'] ? $config['search']['title'] : '['.dr_lang('第%s页', '{page}').'{join}][{keyword}{join}][{param}{join}]黄页{join}{SITE_NAME}';

        if (preg_match_all('/\[.*\{(.+)\}.*\]/U', $meta_title, $m)) {
            $new = '';
            $replace = '';
            foreach ($m[1] as $i => $field) {
                $replace.= $m[0][$i];
                if (isset($data[$field]) && strlen($data[$field])) {
                    $new.= str_replace(array('[', ']'), '', $m[0][$i]);
                }
            }
            $meta_title = str_replace($replace, $new, $meta_title);
        }

        $rep = new \php5replace($data);
        $seo['meta_title'] = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $meta_title);
        $seo['meta_title'] = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), $seo['meta_title']);
        $seo['meta_title'] = trim(str_replace($data['join'].$data['join'], $data['join'], $seo['meta_title']), $data['join']);
        $seo['meta_title'] = preg_replace_callback('#{([a-z_0-9]+)\((.*)\)}#Ui', array($rep, 'php55_replace_function'), $seo['meta_title']);
        unset($rep);

        $seo['meta_title'] = htmlspecialchars(dr_clearhtml($seo['meta_title']));
        $seo['meta_keywords'].= $config['search']['keywords'];

        $seo['meta_keywords'] = trim($seo['meta_keywords'], ',');
        $seo['meta_description'] = $config['search']['description'];
        $seo['meta_description'] = htmlspecialchars(dr_clearhtml($seo['meta_description']));
        $seo['meta_description'] = str_replace('"', '', $seo['meta_description']);


        if (!$seo['meta_keywords']) {
            // 留空时使用主站seo
            $seo['meta_keywords'] = \Phpcmf\Service::C()->get_cache('site', SITE_ID, 'seo', 'SITE_KEYWORDS');
        }

        if (!$seo['meta_description']) {
            // 留空时使用主站seo
            $seo['meta_description'] = \Phpcmf\Service::C()->get_cache('site', SITE_ID, 'seo', 'SITE_DESCRIPTION');
        }

        return $seo;
    }
	public function beijingtu() {
		if(IS_POST){
			$data = dr_safe_replace($_POST['databeijingtu']);
		    
			\Phpcmf\Service::M()->db->table('member_data')->where('id', $this->uid)->update([
			    'beijingtu' => $data,
			]);
			$rt=[
				'code'=>1,
			    'msg'=>'上传成功',
				'data'=>$data,
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		} else {
		    $data = \Phpcmf\Service::M()->db->table('member_data')->where('id', $this->uid)->get()->getRowArray();
		    if($data['beijingtu']){
				$data['beijingtu']=dr_thumb($data['beijingtu']);
			} else {
				$data['beijingtu']='';
			}
			$rt=[
				'code'=>1,
			    'msg'=>'获取成功',
				'data'=>$data,
			];
			return $this->_json($rt['code'], $rt['msg'], $rt['data']);
		}
	}
	

}
